<?php

namespace App\Jobs\Posts;

use App\Models\Posts\WxPost;
use App\Models\WxTenant;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class UpdatePostTenantObjJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    protected $post_id = 0;
    public $tries = 1;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($post_id)
    {
        $this->post_id = $post_id;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        if(_empty_($this->post_id)){
            WxPost::whereNull('tenant_obj')->whereNotNull('tenant_id')->orderBy('id', 'desc')->limit(100)->get()->map(function ($v){
                $the_tenant = WxTenant::where('tenant_id', $v->tenant_id)->first();
                if($the_tenant){
                    WxPost::withoutSyncingToSearch(function () use (&$v, &$the_tenant){
                        WxPost::where('id', $v->id)->update([
                            'tenant_obj' => json_encode([
                                'name' => $the_tenant->name,
                                'short' => $the_tenant->short,
                                'logo' => $the_tenant->logo
                            ])
                        ]);
                    });
                }else{
                    if($v->tenant_id === 0){
                        // 主站都没有设置的情况下
                        WxPost::where('id', $v->id)->update([
                            'tenant_id' => 0,
                            'tenant_obj' => ''
                        ]);
                    }else{
                        WxPost::where('id', $v->id)->update([
                            'tenant_id' => 0
                        ]);
                    }
                }
            });
        }else{
            $post = WxPost::find($this->post_id);
            if($post){
                $the_tenant = WxTenant::where('tenant_id', $post->tenant_id)->first();
                if($the_tenant){
                    WxPost::withoutSyncingToSearch(function () use (&$post, &$the_tenant){
                        WxPost::where('id', $post->id)->update([
                            'tenant_obj' => json_encode([
                                'name' => $the_tenant->name,
                                'short' => $the_tenant->short,
                                'logo' => $the_tenant->logo
                            ])
                        ]);
                    });
                }else{
                    if($post->tenant_id === 0){
                        // 主站都没有设置的情况下
                        WxPost::where('id', $post->id)->update([
                            'tenant_id' => 0,
                            'tenant_obj' => ''
                        ]);
                    }else{
                        WxPost::where('id', $post->id)->update([
                            'tenant_id' => 0
                        ]);
                    }

                }
            }
        }
    }
}
